package cn.smdq.ch.common.core.exception;

import cn.smdq.ch.common.constant.CommonConstants;
import cn.smdq.ch.common.util.ResultUtil;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

/**
 * @author shuimodanqing
 * create at:  2019/1/29  11:34 AM
 * @description 统一异常处理
 */
@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(BaseException.class)
    public ResponseEntity handleBaseException(BaseException e) {
        return ResponseEntity.ok(ResultUtil.error(CommonConstants.ERROR_CODE, e.getMessage()));
    }

    @ExceptionHandler(value = IllegalArgumentException.class)
    public ResponseEntity handleException(IllegalArgumentException e) {
        return ResponseEntity.ok(ResultUtil.error(400, "参数错误:" + e.getLocalizedMessage()));
    }

    @ExceptionHandler(Throwable.class)
    public ResponseEntity handleException(Throwable e) {
        return ResponseEntity.ok(ResultUtil.error(500, "Internal Server Error 服务运行异常:" + e.getMessage()));
    }

}
